package com.openmanus.agent.mapper;

import com.openmanus.agent.core.AgentState;
import com.openmanus.agent.entity.AgentEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 智能体 Mapper 接口
 * 
 * @author OpenManus Team
 * @version 1.0.0
 */
@Mapper
public interface AgentMapper {

    /**
     * 根据ID查找智能体
     * 
     * @param id 智能体ID
     * @return 智能体实体
     */
    @Select("SELECT * FROM t_agents WHERE id = #{id} AND deleted = 0")
    AgentEntity findById(@Param("id") Long id);

    /**
     * 根据名称查找智能体
     * 
     * @param name 智能体名称
     * @return 智能体实体
     */
    @Select("SELECT * FROM t_agents WHERE name = #{name} AND deleted = 0")
    AgentEntity findByName(@Param("name") String name);

    /**
     * 根据类型查找智能体列表
     * 
     * @param agentType 智能体类型
     * @return 智能体实体列表
     */
    @Select("SELECT * FROM t_agents WHERE agent_type = #{agentType} AND deleted = 0")
    List<AgentEntity> findByType(@Param("agentType") String agentType);

    /**
     * 根据状态查找智能体列表
     * 
     * @param state 智能体状态
     * @return 智能体实体列表
     */
    @Select("SELECT * FROM t_agents WHERE state = #{state} AND deleted = 0")
    List<AgentEntity> findByState(@Param("state") AgentState state);

    /**
     * 查找所有智能体
     * 
     * @return 智能体实体列表
     */
    @Select("SELECT * FROM t_agents WHERE deleted = 0 ORDER BY created_at DESC")
    List<AgentEntity> findAll();

    /**
     * 插入智能体
     * 
     * @param agent 智能体实体
     * @return 插入行数
     */
    @Insert("INSERT INTO t_agents (id, name, description, agent_type, state, system_prompt, next_step_prompt, " +
            "max_steps, timeout_seconds, enabled, created_at, updated_at, deleted, version) " +
            "VALUES (#{id}, #{name}, #{description}, #{agentType}, #{state}, #{systemPrompt}, #{nextStepPrompt}, " +
            "#{maxSteps}, #{timeoutSeconds}, #{enabled}, NOW(), NOW(), 0, 1)")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(AgentEntity agent);

    /**
     * 更新智能体
     * 
     * @param agent 智能体实体
     * @return 更新行数
     */
    @Update("UPDATE t_agents SET name = #{name}, description = #{description}, agent_type = #{agentType}, " +
            "state = #{state}, system_prompt = #{systemPrompt}, next_step_prompt = #{nextStepPrompt}, " +
            "max_steps = #{maxSteps}, timeout_seconds = #{timeoutSeconds}, enabled = #{enabled}, " +
            "updated_at = NOW(), version = version + 1 WHERE id = #{id}")
    int update(AgentEntity agent);

    /**
     * 更新智能体状态
     * 
     * @param id 智能体ID
     * @param state 新状态
     * @return 更新行数
     */
    @Update("UPDATE t_agents SET state = #{state}, updated_at = NOW() WHERE id = #{id}")
    int updateState(@Param("id") Long id, @Param("state") AgentState state);

    /**
     * 逻辑删除智能体
     * 
     * @param id 智能体ID
     * @return 删除行数
     */
    @Update("UPDATE t_agents SET deleted = 1, updated_at = NOW() WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

    /**
     * 查找启用的智能体列表
     * 
     * @return 启用的智能体实体列表
     */
    @Select("SELECT * FROM t_agents WHERE enabled = true AND deleted = 0 ORDER BY created_at DESC")
    List<AgentEntity> findEnabledAgents();
}
